﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Infrastructure.Entities.Enum;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Repository;

namespace RooHui.OA.Biz
{
    public class ProcessDoingBiz
    {
        /// <summary>
        /// 启动流程
        /// </summary>
        /// <param name="type"></param>
        /// <param name="projectID"></param>
        /// <param name="processDefID"></param>
        /// <returns></returns>
        public static bool StartProcess(int type, int projectID, int processDefID)
        {
            try
            {
                var processList = new Process().FindAll(c => c.ProcessDefID == processDefID);
                var processDoing = new ProcessDoing();
                foreach (var item in processList)
                {
                    processDoing = new ProcessDoing();
                    processDoing.ProcessID = item.ID;
                    processDoing.ChangeState = item.ChangeState;
                    processDoing.DoingState = -1;
                    processDoing.ProcessName = item.ProcessName;
                    processDoing.ProjectID = projectID;
                    processDoing.Type = type;
                    processDoing.SignType = item.SignType;
                    processDoing.SignUsers = item.SignUsers;
                    processDoing.ChangeTime = DateTime.Now;
                    processDoing.PostAdd();
                }
                var processStepList = new ProcessStep().FindAll(c => c.ProcessDefID == processDefID);
                var processStepDoing = new ProcessStepDoing();
                foreach (var step in processStepList)
                {
                    processStepDoing = new ProcessStepDoing();
                    processStepDoing.CurrentProcessID = step.CurrentProcessID;
                    processStepDoing.NextProcessID = step.NextProcessID;
                    processStepDoing.PreviousProcessID = step.PreviousProcessID;
                    processStepDoing.ProjectID = projectID;
                    processStepDoing.StepName = step.StepName;
                    processStepDoing.Type = type;
                    processStepDoing.PostAdd();
                }

                processStepDoing = new ProcessStepDoing().Find(c => c.CurrentProcessID == -1 && c.ProjectID == projectID);
                processDoing = new ProcessDoing().Find(c => c.ProcessID == processStepDoing.NextProcessID&&c.ProjectID==projectID);
                processDoing.DoingState = 0;
                processDoing.PostModify();

                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return false;
        }
        /// <summary>
        /// 流程办理
        /// </summary>
        /// <param name="projectID"></param>
        /// <param name="processStepDoingID">-1 审核不通过</param>
        /// <param name="nextProcessID"></param>
        /// <returns></returns>
        public static bool ProcessDone(int projectID,int processStepDoingID,string doingContent,string doingUser)
        {
            try
            {
                bool isLast = false;
                var project = new Project().Find(projectID.ToString());
                var processDoingHistory = new ProcessDoingHistory();
                if (processStepDoingID == -1)
                {
                    project.Status = ProjectStatus.NotPass;
                    processDoingHistory.ChangeState = (int)ProjectStatus.NotPass;
                    processDoingHistory.ProcessID = 0;
                    processDoingHistory.Type = 0;
                }
                else
                {
                    var processStepDoing = new ProcessStepDoing().Find(processStepDoingID.ToString());

                    var currnetprocessDoing = new ProcessDoing().Find(c => c.ProjectID == projectID && c.ProcessID == processStepDoing.CurrentProcessID);
                    currnetprocessDoing.DoingState = 1;
                    currnetprocessDoing.ChangeTime = DateTime.Now;
                    currnetprocessDoing.DoneUser = doingUser;
                    currnetprocessDoing.PostModify();
                    if (processStepDoing.NextProcessID > -1)
                    {
                        var nextprocessDoing = new ProcessDoing().Find(c => c.ProjectID == projectID && c.ProcessID == processStepDoing.NextProcessID);
                        nextprocessDoing.DoingState = 0;
                        nextprocessDoing.PostModify();
                    }
                    else
                    {
                        isLast = true;
                    }
                    if (isLast)
                    {
                        project.Status = ProjectStatus.Pass;
                        processDoingHistory.ChangeState = (int)ProjectStatus.Pass;
                    }
                    else
                    {
                        project.Status = ProjectStatus.Auditing;
                        processDoingHistory.ChangeState = (int)ProjectStatus.Auditing;
                    }
                    processDoingHistory.ProcessID = currnetprocessDoing.ID;
                    processDoingHistory.Type = currnetprocessDoing.Type;
                }
                
                project.PostModify();

                //插入历史记录表
                processDoingHistory.DoDirection = processStepDoingID==-1?-1:1;//-1审核不通过
                processDoingHistory.DoingContent = doingContent;
                processDoingHistory.DoingTime = DateTime.Now;
                processDoingHistory.DoingUser = doingUser; 
                processDoingHistory.ProcessStepID = processStepDoingID;
                processDoingHistory.ProjectID = projectID; 
                processDoingHistory.PostAdd();

                if (isLast)//审核通过后，回填相关数据
                {
                    //回填项目数据

                    //回填还款计划表
                }

                return true;
            }
            catch (Exception ex)
            {
                
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return false;
        }
        /// <summary>
        /// 根据项目ID获取流程办理
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public static ProcessDoing GetProcessDoingByProjectID(int projectID)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var data = (from c in ctx.ProcessDoing
                                join d in ctx.User
                                on c.SignUsers equals d.UserName
                                where c.ProjectID == projectID&&c.DoingState==0
                                select new { c, d.RealName })
                             .ToList()
                             .Select(p => new ProcessDoing().Set(p.c).LoadUser(new U_User() { RealName = p.RealName })).FirstOrDefault();

                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }
    }
}
